﻿#region

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Web;
using System.Windows.Forms;
using JCore.LoLChat;

#endregion

namespace JCore
{
    public partial class frmLoLProfiler : Form
    {
        // key: yKazEc5G1UPth1st47JV
        // eclipticus summ id: 36431501

        private static frmLoLProfiler lolProfiler;
        private readonly List<RichTextBox> ChatLogsList = new List<RichTextBox>();
        private readonly Login login = new Login();
        private string SelectedSummonerID = "";
        private string password;
        private string username;

        private frmLoLProfiler()
        {
            InitializeComponent();
        }

        public static frmLoLProfiler getInstance()
        {
            if (lolProfiler == null) lolProfiler = new frmLoLProfiler();
            return lolProfiler;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            RefreshOnlineList();
        }

        public void RefreshOnlineList()
        {
            lstUsers.Items.Clear();

            for (int j = 0; j < login.Friends.Count; j++)
            {
                ListViewItem temp = new ListViewItem();
                int index = -1;
                foreach (FriendsInfo friend in login.FriendsInfo)
                {
                    if (friend.PvpNetID == login.Friends[j].PvpNetID)
                    {
                        index = login.FriendsInfo.IndexOf(friend);
                        break;
                    }
                }
                if (index != -1)
                {
                    temp.Text = login.FriendsInfo[index].profileIcon.ToString(CultureInfo.InvariantCulture);
                    temp.SubItems.Add(login.Friends[j].SummonerName);
                    temp.SubItems.Add(login.FriendsInfo[index].gameStatus);
                    temp.SubItems.Add(HttpUtility.HtmlDecode(login.FriendsInfo[index].statusMsg));
                    lstUsers.Items.Add(temp);
                    lstUsers.Items[lstUsers.Items.Count - 1].Group = lstUsers.Groups[0];
                }
                else
                {
                    temp.SubItems.Add(login.Friends[j].SummonerName);
                    temp.SubItems.Add("Offline");
                    lstUsers.Items.Add(temp);
                    lstUsers.Items[lstUsers.Items.Count - 1].Group = lstUsers.Groups[1];
                }
            }
        }

        private void frmLoLProfiler_Load(object sender, EventArgs e)
        {
            username = "jipjan2";
            password = "jaapjan95";
            try
            {
                login.Connect(username, password, this);
            }
            catch (Exception)
            {
                MessageBox.Show("Couldn't login, sorry =(");
            }
        }

        public void MessageToChatLog(string text, string person, string JID)
        {
            // Persoon 1 bericht
            // Geen tag, lijst 0
            Console.WriteLine(txtChatLog.Tag);
            // If tag is same or list doesn't contains items
            if ((string) txtChatLog.Tag == person || ChatLogsList.Count == 0)
            {
                // set tag
                ChatLogsList.Add(txtChatLog);
                txtChatLog.Tag = person;
                SelectedSummonerID = JID;
                lblTo.Text = "Send To: " + person;
            }
                // if tag is not the same or list does contain items
            else
            {
                int index = -1;
                foreach (RichTextBox ChatLog in ChatLogsList)
                {
                    index += 1;
                    if (ChatLog.Tag == txtChatLog.Tag)
                    {
                        ChatLogsList[index] = txtChatLog;
                        break;
                    }
                    else
                    {
                        ChatLogsList.Add(txtChatLog);
                    }
                }

                // Add to list and clear log (tag in place)
                foreach (RichTextBox ChatLog in ChatLogsList)
                {
                    if ((string) ChatLog.Tag == person)
                    {
                        txtChatLog = ChatLog;
                        break;
                    }
                    else
                    {
                        txtChatLog.Tag = person;
                        txtChatLog.Text = "";
                    }
                }
            }
            // Always add the text...
            if (text != "")
            {
                txtChatLog.AppendText(person + ": ", Color.Blue);
                txtChatLog.AppendText(DateTime.Now.ToString("HH:mm:ss tt"), Color.Gray);
                txtChatLog.AppendText(" - ");
                txtChatLog.AppendText(text + Environment.NewLine, Color.Blue);
            }
        }

        public void AddToFlowLayout(string summoner, string id)
        {
            Button chatButton = new Button();
            chatButton.Text = summoner;
            chatButton.Name = id + "@pvp.net";
            if (flwLayoutChats.Controls.Count == 0)
            {
            }
            chatButton.MouseClick += OnButtonClick;

            flwLayoutChats.Controls.Add(chatButton);
        }


        private void OnButtonClick(object sender, EventArgs e)
        {
            Button button = sender as Button;
            SelectedSummonerID = button.Name;
            lblTo.Text = "Send To: " + button.Text;
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            label1.Text = login.Friends.Count.ToString(CultureInfo.InvariantCulture);
            label2.Text = login.FriendsInfo.Count.ToString(CultureInfo.InvariantCulture);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (SelectedSummonerID != "" && txtChatMessage.Text != "")
            {
                txtChatLog.AppendText(username + ": ", Color.Red);
                txtChatLog.AppendText(DateTime.Now.ToString("HH:mm:ss tt"), Color.Gray);
                txtChatLog.AppendText(" - ");
                txtChatLog.AppendText(txtChatMessage.Text + Environment.NewLine, Color.Red);
                login.SendMessage(txtChatMessage.Text, SelectedSummonerID);
                txtChatMessage.Text = "";
            }
        }

        private void lstUsers_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lstUsers.SelectedItems[0].Group.Header == "Online")
            {
                foreach (Friend friend in login.Friends)
                {
                    if (friend.SummonerName == lstUsers.SelectedItems[0].SubItems[1].Text)
                    {
                        MessageToChatLog("", lstUsers.SelectedItems[0].SubItems[1].Text, friend.PvpNetID + "@pvp.net");
                    }
                }
            }
        }
    }

    #region RichTextBox Extension for Color

    public static class RichTextBoxExtensions
    {
        public static void AppendText(this RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }
    }

    #endregion
}